<template>
    <section>

        <!--高级查询-->
        <el-col :span="24" class="toolbar" style="padding-bottom: 0px;">
            <el-form :inline="true" :model="searchForm">
                <el-form-item>
                    <el-input v-model="searchForm.name" placeholder="姓名"></el-input>
                </el-form-item>
                <el-form-item>
                    <el-select v-model="searchForm.state" clearable placeholder="请选择状态">
                        <el-option label="上架" value="1"></el-option>
                        <el-option label="下架" value="0"></el-option>
                    </el-select>
                </el-form-item>
                <el-form-item>
                    <el-button type="primary" v-on:click="getProducts">查询</el-button>
                </el-form-item>
                <el-form-item>
                    <el-button type="primary" @click="handleAdd">新增</el-button>
                    <el-button type="primary" @click="onsale" :disabled="this.sels.length===0">上架</el-button>
                    <el-button type="primary" @click="offsale" :disabled="this.sels.length===0">下架</el-button>
                </el-form-item>
            </el-form>
        </el-col>

        <!--列表-->
        <el-table :data="products" highlight-current-row v-loading="listLoading" @selection-change="selsChange"style="width: 100%;">
            <el-table-column type="selection" width="55">
            </el-table-column>
            <el-table-column type="index" width="60" label="序号">
            </el-table-column>
            <el-table-column prop="name" label="服务名">
            </el-table-column>
            <el-table-column prop="saleprice" label="售价" >
            </el-table-column>
            <el-table-column prop="offsaletime" label="下架时间" >
            </el-table-column>
            <el-table-column prop="onsaletime" label="上架时间" >
            </el-table-column>
            <el-table-column prop="state" label="状态" :formatter="formatSex">
            </el-table-column>
            <el-table-column prop="costprice" label="成本价" >
            </el-table-column>
            <el-table-column prop="createtime" label="创建时间" >
            </el-table-column>
            <el-table-column prop="salecount" label="销量" >
            </el-table-column>
            <el-table-column label="操作" width="150">
                <template scope="scope">
                    <el-button size="small" @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
                    <el-button type="danger" size="small" @click="handleDel(scope.$index, scope.row)">删除</el-button>
                </template>
            </el-table-column>
        </el-table>

        <!--分页工具条-->
        <el-col :span="24" class="toolbar">
            <el-button type="danger" @click="batchRemove" :disabled="this.sels.length===0">批量删除</el-button>
            <el-pagination layout="prev, pager, next" @current-change="handleCurrentChange" :page-size="pageSize" :total="total" style="float:right;">
            </el-pagination>
        </el-col>
        <!--新增/编辑模态框-->
        <el-dialog :title="title" :visible.sync="addFormVisible" :close-on-click-modal="false">
            <el-form :model="addForm" label-width="80px" :rules="rules" ref="ruleForm">
                <el-form-item label="服务名" prop="name">
                    <el-input v-model="addForm.name" auto-complete="off"></el-input>
                </el-form-item>
                <el-form-item label="零售价" >
                    <el-input v-model="addForm.saleprice"  auto-complete="off"></el-input>
                </el-form-item>
                <el-form-item label="成本价" >
                    <el-input v-model="addForm.costprice"  auto-complete="off"></el-input>
                </el-form-item>
               <!-- <el-form-item label="产品图片" prop="resources">
                    &lt;!&ndash;手动上传&ndash;&gt;
                    <el-upload
                            ref="upload"
                            :auto-upload="false"
                            class="upload-demo"
                            action="/fastDfs/upload"
                            :http-request="uploadFile"
                            :on-success="handleSuccess"
                            :file-list="fileList"
                            list-type="picture">
                        <el-button size="small" type="primary">点击上传</el-button>
                        <div slot="tip" class="el-upload__tip">只能上传jpg/png文件，且不超过500kb</div>
                    </el-upload>
                </el-form-item>-->
                <el-form-item label="服务详情">
                    <!--<el-input v-model="addForm.intro"  auto-complete="off"></el-input>-->
                    <quill-editor v-model="addForm.intro"  :options="editorOption">
                    </quill-editor>
                </el-form-item>
                <el-form-item label="预约须知">
                    <quill-editor v-model="addForm.orderNotice"  :options="editorOption">
                    </quill-editor>
                </el-form-item>
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button @click.native="addFormVisible = false">取消</el-button>
                <el-button type="primary" @click.native="addSubmit" >提交</el-button>
            </div>
        </el-dialog>

        </section>
</template>



<script>
    import { quillEditor } from "vue-quill-editor"; //调用编辑器
    import 'quill/dist/quill.core.css';
    import 'quill/dist/quill.snow.css';
    import 'quill/dist/quill.bubble.css';
    import quillConfig from "../../common/quill-config"



    export default {
        components: {
            quillEditor
        },
        data() {
            return {
                editorOption: quillConfig,
                rules: {
                    name: [
                        {required: true, message: '服务名必填', trigger: 'blur'}
                    ],
                },
                //图片数据回显
                fileList: [],
                //新增/编辑的form表单
                addForm: {
                    id:"",
                    //服务名
                    name:"",
                    //销售价
                    saleprice:"0",
                    //成本价
                    costprice:"0",
                    //产品图片
                    resources:"",
                    //服务描述
                    intro:"",
                    //预约须知
                    orderNotice:""
                },
                //新增和编辑模态框默认为不弹出
                addFormVisible: false,
                //新增和编辑标题
                title: "",
                //高级查询表单数据
                searchForm: {
                    name: "",
                    state: ""
                },
                //是否加载
                listLoading: false,
                ////列表选中列
                sels: [],
                //列表展示
                products: [],
                //每页条数
                pageSize: 10,
                //当前页
                currentPage: 1,
                //总条数
                total: 0
            }
        },
        methods: {
            //上架
            onsale(){
                this.$confirm('你确定要上架吗?', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(() => {//点击确定执行里面的代码
                    this.$http.patch("/product/onsale", this.sels).then(res => {
                        //回调函数和解构表达式
                        let {success, msg} = res.data
                        if (success) {
                            this.$message({
                                type: success,
                                message: '上架成功'
                            });
                            //删除成功，重新加载列表
                            this.loadProducts();
                        } else {//打印错误
                            this.$message.error(msg);
                        }
                    })
                }).catch(() => {//点击取消执行里面代码
                });
            },
            //下架
            offsale(){
                this.$confirm('你确定要下架吗?', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(() => {//点击确定执行里面的代码
                    this.$http.patch("/product/offsale", this.sels).then(res => {
                        //回调函数和解构表达式
                        let {success, msg} = res.data
                        if (success) {
                            this.$message({
                                type: success,
                                message: '下架成功'
                            });
                            //删除成功，重新加载列表
                            this.loadProducts();
                        } else {//打印错误
                            this.$message.error(msg);
                        }
                    })
                }).catch(() => {//点击取消执行里面代码
                });
            },
            //自定义上传文件
            uploadFile(params){
                // 通过 FormData 对象上传文件
                var formData = new FormData();
                formData.append("file", params.file);

                //发送请求上传附件
                this.$http.post(params.action,formData).then(res=>{
                    let {success,msg,objResult}=res.data;
                    //给resources赋值
                    this.addForm.resources=objResult;
                    //调用save方法发送请求保存入驻商家信息
                    this.save();
                })
            },
            /*上传文件的成功方法*/
            handleSuccess(response, file, fileList){
                console.log(response);
                this.addForm.resources = response.objResult;
            },

            //给新增和编辑模态框的提交按钮注册事件
            addSubmit() {
                this.$refs.ruleForm.validate((valid) => {
                    //校验表单成功后才做一下操作
                    if (valid) {

                            /*//判断是否上传的图片
                            if(this.$refs.upload.uploadFiles.length){
                                //手动提交图片附件
                                this.$refs.upload.submit();
                            }else {
                                this.save();
                            }*/

                            let productDetail={"intro":this.addForm.intro,
                                               "orderNotice" :this.addForm.orderNotice};
                            //给addForm添加productDetail属性
                            this.addForm.productDetail=productDetail;
                            //发送请求保存数据
                        this.$http.post("/product",this.addForm).then(res=>{
                            let{success,msg}=res.data;
                            if(success){
                                this.$message({
                                    type: success,
                                    message: '操作成功'
                                });
                                //关闭模态框
                                this.addFormVisible=false;
                                //添加成功，重新加载列表
                                this.loadProducts();
                            }else{//打印错误
                                this.$message.error(msg);
                            }
                        })

                    }
                })
            },
            //给新增按钮绑定事件
            handleAdd() {
                //给新增标题赋值
                this.title="新增";
                //重置表单校验属性
                if(this.$refs["ruleForm"]) {
                    this.$refs["ruleForm"].resetFields();
                }
                //重置普通属性
                for(var i in this.addForm){
                    this.addForm[i]="";
                }
                //弹出新增模态框
                this.addFormVisible=true;
            },
            //给高级查询按 钮绑定事件
            getProducts() {
                //设置查询页码为第一页
                this.currentPage = 1;
                //加载查询的部门列表
                this.loadProducts();
            },
            //勾选复选框时触发此函数
            selsChange(sels) {
                /*console.debug(sels)*/
                this.sels = sels;
            },
            //批量删除
            batchRemove() {
                /*console.debug(this.sels)*/
                this.$confirm('此操作将永久删除该数据, 是否继续?', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(() => {//点击确定执行里面的代码
                    this.$http.patch("/product/delete", this.sels).then(res => {
                        //回调函数和解构表达式
                        let {success, msg} = res.data
                        if (success) {
                            this.$message({
                                type: success,
                                message: '删除成功'
                            });
                            //删除成功，重新加载列表
                            this.loadProducts();
                        } else {//打印错误
                            this.$message.error(msg);
                        }
                    })
                }).catch(() => {//点击取消执行里面代码
                });
            },
            //页码改变时，执行此函数
            handleCurrentChange(val) {
                this.currentPage = val
                //继续分页查询
                this.loadProducts();
            },
            //上下架状态显示转换
            formatSex(row) {
                return row.state == 1 ? "上架" : "下架"
            },

            //给编辑按钮添加事件
            handleEdit(index, row) {
                console.debug(row)
                //给标题设置值
                this.title="编辑"
                //回显普通数据
                for(var i in this.addForm){
                    this.addForm[i]=row[i]
                };
                //特定数据回显
                let para=row.resources.split(",")
                for(var i=0;i<para.length;i++){
                    this.fileList.push({"url":"http://115.159.217.249:8888"+para[i]})
                }
                this.addForm.intro=row.productDetail.intro
                this.addForm.orderNotice=row.productDetail.orderNotice
                //弹出模态框
                this.addFormVisible=true;
            },
            //给删除按钮添加事件
            handleDel(index, row) {
                console.debug(index, row)
                this.$confirm('此操作将永久删除该数据, 是否继续?', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(() => {//点击确定执行里面的代码
                    this.$http.delete("/product/" + row.id).then(res => {
                        //回调函数和解构表达式
                        let {success, msg} = res.data
                        if (success) {
                            this.$message({
                                type: success,
                                message: '删除成功'
                            });
                            //删除成功，重新加载列表
                            this.loadProducts();
                        } else {//打印错误
                            this.$message.error(msg);
                        }
                    })
                }).catch(() => {//点击取消执行里面代码
                });
            },
            //加载服务列表
            loadProducts() {
                //显示加载
                this.listLoading = true;
                let parm = {
                    "currentPage": this.currentPage,
                    "pageSize": this.pageSize,
                    "name": this.searchForm.name,
                    "state": this.searchForm.state
                }
                this.$http.patch("/product", parm).then(res => {
                    //关闭加载
                    this.listLoading = false;
                    /*console.debug(res)*/
                    let {total, list} = res.data
                    this.total = total,
                        this.products = list
                })
            },
        },
            mounted() {
                //加载分页服务列表
                this.loadProducts();
            }

    }
</script>